package ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import dao.CustomerDao;
import entity.Customer;

public class CustomerLoginFrame extends JFrame {

	private JPanel contentPane;
	private JTextField idTextField;
	private JTextField passwordTextField;
	private JButton loginButton;
	private JButton registerButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CustomerLoginFrame frame = new CustomerLoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CustomerLoginFrame() {
		setTitle("登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 519, 340);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblId = new JLabel("ID：");
		
		idTextField = new JTextField();
		idTextField.setColumns(10);
		
		JLabel label = new JLabel("密码：");
		
		passwordTextField = new JTextField();
		passwordTextField.setColumns(10);
		
		loginButton = new JButton("登录");
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginActionPerformed(e);
			}
		});
		
		registerButton = new JButton("注册");
		/**
		 * 监听注册按钮
		 */
		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CustomerRegisterFrame frame = new CustomerRegisterFrame();
                frame.setVisible(true);
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(92, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(registerButton)
							.addGap(32)
							.addComponent(loginButton)
							.addGap(35))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblId)
								.addComponent(label))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(idTextField)
								.addComponent(passwordTextField, GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
							.addGap(34)))
					.addGap(84))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(52, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(idTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblId))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(passwordTextField, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(label))
					.addGap(27)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(loginButton)
						.addComponent(registerButton))
					.addContainerGap(57, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
	/**
	 * 登录按钮的实现
	 * @param e
	 */
	private Customer loginActionPerformed(ActionEvent e) {
		//验证用户ID和密码是否匹配
        int id = -1;//初始化一下，以便等会儿判空
        String temp = this.idTextField.getText();
        if(!temp.isEmpty()) {
            id = Integer.parseInt(this.idTextField.getText());
        } else {
        	JOptionPane.showMessageDialog(null, "ID不能为空！");
        	return null;//id为空则返回
        }
        String password = this.passwordTextField.getText();
        if(password.isEmpty()) {
        	JOptionPane.showMessageDialog(null,"密码不能为空！");
        	return null;
        }
		CustomerDao customerDao = new CustomerDao();
		Customer customer = customerDao.testLogin(id, password);
		if(customer == null) {
			JOptionPane.showMessageDialog(null, "登陆失败，请检查用户名和密码！");
			return null;
		} else {
			JOptionPane.showMessageDialog(null, "登录成功！");
		}
		return customer;
	}
}
